package com.tong.exception;

import com.tong.result.Response;
import com.tong.result.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author tong
 * @date 2021/7/29 16:06
 * 统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult err(Exception e) {
        e.printStackTrace();
        return Response.makeRsp(500, "系统异常");
    }

    /**
     * 自定义
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResponseResult err(MyException e) {
        return Response.makeRsp(400, e.getMessage());
    }
}
